var G={
  width:0,
  height:0,
  left:0,
  top:0,
  dom : function(selector){
    return document.querySelector(selector);
  },
  show : function(el){
    el.style.display = 'block';
  },
  hide : function(el){
    el.style.display = 'none';
  },
  resize:function($p,h,w){
  	// w = 5,h = 9;
  	/**
  	 * 获取得到窗口尺寸
  	 */
  	var winWidth=Math.max(window.innerWidth || 0,document.body.clientWidth  || 0,document.documentElement.clientWidth  || 0);
  	var winHeight=Math.max(window.innerHeight || 0,document.body.clientHeight || 0,document.documentElement.clientHeight || 0);
  	var width,height,left,top;
  	// winWidth/winHeight = h/w => winWidth*w = winHeight*h
  	if(winWidth*w > winHeight*h){
  	  // 如果宽度太宽:以高度来计算
  	  width = (winHeight*h)/w;
  	  height = winHeight;
  	  left = (winWidth-width)/2;
  	  top = 0;
  	}else{
  	  // 如果高度太高:以宽度来计算
  	  width = winWidth;
  	  height = (winWidth*w)/h;
  	  left = 0;
  	  top = (winHeight-height)/2;
  	}
  	$p.style.marginLeft = left + 'px';
  	$p.style.marginTop = top + 'px';
  	$p.style.width = width +'px';
  	$p.style.height = height +'px';

  	G.width=width;
  	G.height=height;
  	G.left=left;
  	G.top=top;
  },
  randomNum:function(min,max){
  	return Math.floor( Math.random()*(max-min+1)+min );
  },
  type:function(data){
  	return Object.prototype.toString.call(data).slice(8,-1).toLowerCase();
  },
  empty:function(data){
  	if (typeof data === 'object') {
  	  for (var prop in data ) {
  	    return false;
  	  }
  	}
  	return !data;
  },
  extend:function(des, src, override){
  	var i = 0,l = 0,k = null;
  	if ( src instanceof Array ) {
  	  for ( i = 0, l = src.length; i < l; i++ ) {
  	  	G.extend(des, src[i], override);
  	  }
  	} else {
  	  for ( k in src ) {
  	  	if ( override || !(k in des) ) {
  	  	  des[k] = src[k];
  	  	}
  	  }
  	}
  	return des;
  },
  override:function(des,src){
  	G.extend(des,src||{},true);
  },
  offset:function(selector,context){
  	context = context || document;
  	var el = App.isElement(selector)?selector:context.querySelector(selector);
  	if(!el)return;
  	var sl = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
  	var st = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  	var rect = el.getBoundingClientRect();
  	return {l : rect.left+sl, t : rect.top+st,w : el.offsetWidth, h : el.offsetHeight};
  },
  event_offset:function(e,w,h){
    var tempx = e.offsetX==null?e.layerX:e.offsetX;
    var tempy = e.offsetY==null?e.layerY:e.offsetY;
    var offsetX = tempx*w/G.width;
    var offsetY = tempy*h/G.height;
    return {offsetX:offsetX,offsetY:offsetY};
  },
  // 判断两个坐标是否相同
  equals:function(x1,y1,x2,y2){
  	return x1==x2 && y1==y2;
  },
  // 获取两个坐标直接的距离
  distance:function(x1,y1,x2,y2){
  	return Math.sqrt( Math.pow(x1-x2,2) + Math.pow(y1-y2,2) );
  },
  // 点碰撞矩形检测
  point_rect:function(x1,y1,x2,y2,w2,h2){
  	return x1>x2 && x1<x2+w2 && y1>y2 && y1<y2+h2;
  },
  // 矩形碰撞矩形检测【逆推】
  rect_rect:function(x1,y1,w1,h1,x2,y2,w2,h2){
  	return x1<=x2+w2 && x2<=x1+w1 && y1<=y2+h2 && y2<=y1+h1;
  },
  // 矩形碰撞圆形检测【逆推】
  rect_circ:function(x1,y1,w1,h1,x2,y2,r2){
  	return x2-r2<=x1+w1 && x1<=x2+r2 && y2-r2<=y1+h1 && y1<=y2+r2;
  },
  // 点碰撞圆形检测
  point_circ:function(x1,y1,x2,y2,r2){
  	return G.distance(x1,y1,x2,y2) < r2;
  },
  // 圆形碰撞圆形检测
  circ_circ:function(x1,y1,r1,x2,y2,r2){
  	return G.distance(x1,y1,x2,y2) <= r1+r2;
  }
};
/**
 * 类
 */
G.load = function(){
  var s = this;

  s.type = 'load';

  s.images = {}; // 图片对象
  s.medias = {}; // 媒体对象
  s.progress = 0; // 进度(小数)
  s.index = 0; // 当前进度(索引)
  s.total = 0; // 总进度(总长度)
};
G.load.prototype = {
  media : function(res,progress,result){
    var s = this,i = 0,l = res.length,temp,pgStatus = (typeof progress === 'function');

    s.total = l;

    var resLoad = function(){
      s.index++;
      s.progress = s.index/s.total;

      if(pgStatus){
        progress(s.progress);
      }
      if(s.index>=s.total){
        // 最后一张图片
        if(typeof result==='function'){
          resLoad = null;
          result(s.medias);
        }
      }
    }

    for(;i<l;i++){
      if(res[i]['type'] == 'img'){
        temp = new Image();
        s.medias[ res[i]['name'] ] = temp;
        temp.src = res[i]['path'];
        if(temp.complete){
          resLoad();
          continue;
        }
        temp.onload = resLoad;
        temp.onerror = resLoad;
      }else if(res[i]['type'] == 'audio'){
        temp = document.createElement('audio');
        s.medias[ res[i]['name'] ] = temp;
        temp.src = res[i]['path'];
        temp.onloadeddata = resLoad;
        temp.onerror = resLoad;
      }
    }
  }
};

/**
 * 绘制路径
 */
// 绘制圆角矩形
G.drawRoundRect = function(c,pa){
  c.beginPath();
  c.moveTo(pa[0] + pa[4], pa[1]);
  c.lineTo(pa[0] + pa[2] - pa[4], pa[1]);
  c.arcTo(pa[0] + pa[2], pa[1], pa[0] + pa[2], pa[1] + pa[4], pa[4]);
  c.lineTo(pa[0] + pa[2], pa[1] + pa[3] - pa[4]);
  c.arcTo(pa[0] + pa[2], pa[1] + pa[3], pa[0] + pa[2] - pa[4], pa[1] + pa[3], pa[4]);
  c.lineTo(pa[0] + pa[4], pa[1] + pa[3]);
  c.arcTo(pa[0], pa[1] + pa[3], pa[0], pa[1] + pa[3] - pa[4], pa[4]);
  c.lineTo(pa[0], pa[1] + pa[4]);
  c.arcTo(pa[0], pa[1], pa[0] + pa[4], pa[1], pa[4]);
  c.closePath();
};